﻿using SuperMoney.Datas.DbModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SuperMoney.Datas
{
    /// <summary>
    /// 消费订单数据服务。
    /// </summary>
    public class ConsumeOrderDbService
    {
        /// <summary>
        /// 消费订单数据服务实例。
        /// </summary>
        public static readonly ConsumeOrderDbService Instance = new ConsumeOrderDbService();

        /// <summary>
        /// 创建消费订单。
        /// </summary>
        /// <param name="consumeOrder">消费订单新实例。</param>
        public void Create(ConsumeOrder consumeOrder)
        {
            consumeOrder.AddTime = DateTime.Now;
            consumeOrder.UpdateTime = DateTime.Now;

            using (var db = MRMDB.New())
            {
                db.ConsumeOrderSet.Add(consumeOrder);
                db.SaveChanges();
            }
        }

        /// <summary>
        /// 获取消费订单。
        /// </summary>
        /// <param name="id">订单编号。</param>
        /// <param name="db">数据上下文。</param>
        /// <returns></returns>
        internal ConsumeOrder Get(long id, MRMEntities db)
        {
            return db.ConsumeOrderSet.Where(p => p.Id == id).SingleOrDefault();
        }

        /// <summary>
        /// 获取单个订单记录信息
        /// </summary>
        /// <param name="orderId">订单编号</param>
        /// <returns>指定Id的订单信息</returns>
        public ConsumeOrder GetSingleOrder(long orderId)
        {
            using (var db = MRMDB.New())
            {
                return this.Get(orderId, db);
            }
        }

        /// <summary>
        /// 获取指定状态的记录条数
        /// </summary>
        /// <param name="status">指定的状态</param>
        /// <returns></returns>
        public int Count(int status)
        {
            using (var db = MRMDB.New())
            {
                return db.ConsumeOrderSet.Where(p => p.Status == status).Count();
            }
        }

        /// <summary>
        /// 获取消费订单。
        /// </summary>
        /// <param name="deviceId">设备编号。</param>
        /// <param name="count">每页条数。</param>
        /// <param name="page">当前页码。</param>
        /// <returns></returns>
        public List<ConsumeOrder> GetList(string deviceId, int count, int page)
        {
            using (var db = MRMDB.New())
            {
                int skip = (page - 1) * count;
                return db.ConsumeOrderSet.Where(p => p.DeviceId == deviceId).OrderByDescending(p => p.Id).Skip(skip).Take(count).ToList();
            }
        }

        /// <summary>
        /// 获取用户的订单列表
        /// </summary>
        public List<ConsumeOrder> GetOrders(int pageSize, int pageIndex, int status)
        {
            using (var db = MRMDB.New())
            {
                int skip = (pageIndex - 1) * pageSize;
                return db.ConsumeOrderSet.Where(p => p.Status == status).OrderByDescending(p => p.AddTime).Skip(skip).Take(pageSize).ToList();
            }
        }


        /// <summary>
        /// 更新消费订单状态。
        /// </summary>
        /// <param name="id">订单编号。</param>
        /// <param name="status">订单状态。</param>
        public void UpdateStatus(long id, int status)
        {
            using (var db = MRMDB.New())
            {
                var consumeOrder = this.Get(id, db);
                consumeOrder.Status = status;
                consumeOrder.UpdateTime = DateTime.Now;
                db.SaveChanges();
            }
        }
    }
}
